package com.rytong.tools.ui.jsfunction;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import android.app.Activity;
import android.text.TextUtils;

import com.rytong.tools.offstore.OffStoreDB;
import com.rytong.tools.offstore.OffStoreDownload;
import com.rytong.tools.ui.LPWebView.MyLPWebView;
import com.rytong.tools.utils.Utils;

public class EmpFile {
	final String UNKNOWN_ERROR = "1";
	final String WRITE_FAIL_ERROR = "2";
	final String READ_FAIL_ERROR = "3";
	final String REMOVE_FAIL_ERROR = "4";

	/**
	 * write接口写的文件，必要保存到单独的路径下。与离线资源，插件资源的路径分开.
	 * 
	 * 即：OffStoreDownload.WRITEROOT
	 * */
	public void write(final MyLPWebView ewv, Object name, Object data,
			String callback) {

		boolean flag = true;

		if (name == null || data == null) {
			flag = false;
		} else {
			String fileName = null;
			FileOutputStream fos = null;
			if (name instanceof String)
				fileName = (String) name;
			if (fileName != null) {
				try {
					byte[] bytes = null;
					String root = OffStoreDownload.WRITEROOT;
					Utils.saveFile(root, bytes);
					if (data instanceof String) {
						bytes = ((String) data).getBytes();
					} else {
						bytes = (byte[]) data;
					}
					Utils.saveFile(root.concat(fileName), bytes);
				} catch (Exception e) {
					flag = false;
					Utils.printException(e);
				} finally {
					if (fos != null) {
						try {
							fos.flush();
							fos.close();
							fos = null;
						} catch (IOException e) {
							Utils.printException(e);
						}
					}
				}
			} else {
				flag = false;
			}
		}

		if (!TextUtils.isEmpty(callback)) {
			callback = "var writemp = " + callback + "; ";

			if (flag) {// 写入文件成功 标志位 0
				callback += " writemp(0);";
			} else {
				callback += " writemp(1);";
			}
			doCallback(ewv, callback);
		}
	}

	/**
	 * 插件 > 离线 > file:write > 安装包
	 * */
	public void isExist(final MyLPWebView ewv, Object name, String callback) {

		String errorCode = "0";
		boolean isExist = true;

		String fileName = null;
		if (null == name) {
			isExist = false;
			errorCode = "1";
		} else {
			if (name instanceof String) {
				fileName = (String) name;
			}

			if (!TextUtils.isEmpty(fileName)) {
				byte[] buffer = null;
				buffer = Utils.readLocalFile(fileName);
				if (null == buffer) {
					isExist = false;
				}
			} else {
				isExist = false;
				errorCode = "2";
			}

		}

		if (!TextUtils.isEmpty(callback)) {
			callback = "var isexistmp = " + callback + "; ";

			if (isExist) {
				doCallback(ewv,
						callback.concat(" isexistmp(" + errorCode + " ,true);"));
			} else {
				doCallback(ewv, callback.concat(" isexistmp(" + errorCode
						+ " ,false);"));
			}
		}
	}

	/**
	 * 插件 > 离线 > file:write > 安装包
	 * */
	public void read(MyLPWebView ewv, Object name, Object type, String callback) {

		String errorCode = "0";
		String content = null;

		String fileName = null;
		String fileType = null;

		String jsfun = "";

		if (name == null) {
			errorCode = "1";
			jsfun = " readtmp(" + errorCode + " , '');";
		} else {

			byte[] buffer = null;

			if (name instanceof String)
				fileName = (String) name;
			if (type instanceof String)
				fileType = (String) type;

			if(!TextUtils.isEmpty(fileName)){
				    buffer = Utils.readLocalFile(fileName);

				if (buffer != null) {
					if (fileType != null && fileType.equals("text")) {
						try {
							content = new String(buffer, "UTF-8");
						} catch (UnsupportedEncodingException e) {
							errorCode = "4";
							content = null;
							Utils.printException(e);
						}
						jsfun = " readtmp(" + errorCode + " , '" + content
								+ "');";
					} else {
						jsfun = " readtmp(" + errorCode + " , " + buffer + ");";
					}
				} else {
					errorCode = "3";
					jsfun = " readtmp(" + errorCode + " , '');";
				}
			} else {
				errorCode = "2";
				jsfun = " readtmp(" + errorCode + " , '');";
			}

		}

		if (!TextUtils.isEmpty(callback)) {
			callback = "var readtmp = " + callback + "; " + jsfun;
			doCallback(ewv, callback);
		}

	}

	/**
	 * 只能删除file:write写进去的文件
	 * */
	public void remove(MyLPWebView ewv, Object name, String callback) {

		String errorCode = "0";

		String fileName = null;
		if (null == name) {
			errorCode = "1";
		} else {
			if (name instanceof String) {
				fileName = (String) name;
			}

			if (fileName != null) {
				File file = new File(OffStoreDownload.FILEROOT.concat(
						OffStoreDownload.WRITEROOT).concat(fileName));
				if (file.exists()) {
					file.delete();
				}
			} else {
				errorCode = "2";
			}
		}

		if (!TextUtils.isEmpty(callback)) {
			callback = "var removetmp = " + callback + "; removetmp("
					+ errorCode + ");";
			doCallback(ewv, callback);
		}
	}

	private void doCallback(final MyLPWebView ewv, final String callback) {

		if (!TextUtils.isEmpty(callback)) {
			((Activity) ewv.getContext()).runOnUiThread(new Runnable() {

				@Override
				public void run() {
					ewv.loadUrl("javascript:".concat(callback));

				}
			});

		}
	}
}
